package com.itboye.mengwu.app.ui.aiwujia;

import java.nio.channels.AlreadyConnectedException;
import java.nio.channels.NonWritableChannelException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.luntan.comment.adapter.CommentAdapter;

import android.R.string;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.LinearGradient;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

/**
 *
 * 
 * 2016.5.04
 * 
 */
public class FragementMomZeng extends BaseFragment implements OnItemClickListener, Observer {
	private ListView listview;
	List<ImageView> list;
	TieZiPresenter presenter;
	LuntanTieZiBean bean;
	private ArrayList<GetAList> aLists;
	private MyAdapter adapter;

	@Override
	public int initView() {

		return R.layout.cast_of_love_layout2;
	}

	@Override
	public void onMyActivityCreated() {
		showProgressDialog("数据加载中，请稍后", true);
		aLists = new ArrayList<TieZiBaoHanReply.GetAList>();
		presenter = new TieZiPresenter(this);
		bean = new LuntanTieZiBean();
		bean.setPage_no(1 + "");
		bean.setFid(5 + "");
		String uid = SPUtils.get(getActivity(), null, SpPublicParams.UID, "") + "";
		if (uid != null || uid != "")
			bean.setUid(uid);
		presenter.getInfo(bean);
		listview = (ListView) getActivity().findViewById(R.id.castlove_list_2);
		adapter = new MyAdapter(getActivity());
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(this);
	}

	class MyAdapter extends BaseAdapter {
		Context context;

		public MyAdapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			return aLists == null ? 0 : aLists.size();
		}

		@Override
		public Object getItem(int position) {
			return aLists == null ? 0 : aLists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater inflater = LayoutInflater.from(context);
				convertView = inflater.inflate(R.layout.zengwuxian_layout, null);
				holder.username_text = (TextView) convertView.findViewById(R.id.username_text);
				holder.update_time = (TextView) convertView.findViewById(R.id.update_time);
				holder.zeng_text = (TextView) convertView.findViewById(R.id.zeng_text);
				holder.address_text = (TextView) convertView.findViewById(R.id.address_text);
				holder.head_image = (ImageView) convertView.findViewById(R.id.head_image);
				holder.imgContainer = (LinearLayout) convertView.findViewById(R.id.imgContainer);
				holder.image1 = (ImageView) convertView.findViewById(R.id.img_1);
				holder.image2 = (ImageView) convertView.findViewById(R.id.img_2);
				holder.image3 = (ImageView) convertView.findViewById(R.id.img_3);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.username_text.setText(aLists.get(position).getAuthor());

			SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy-MM-dd");// 年月日的格式
			SimpleDateFormat myFmt2 = new SimpleDateFormat("HH:mm:ss");// 时分秒的格式
			SimpleDateFormat myFmt3 = new SimpleDateFormat("m分钟前");// 最后做时间差用的分钟
			SimpleDateFormat myFmt4 = new SimpleDateFormat("H小时前");// 最后做时间差用的小时
//			Log.e("!!!!!!", position + "");
//			Log.e("!!!!!!", myFmt1.format(Long.parseLong((aLists.get(position).getDateline()) + "") * 1000));// 获取该position下的帖子年月日
//			Log.e("!!!!!!", myFmt2.format(Long.parseLong((aLists.get(position).getDateline()) + "") * 1000));// 获取该position下的帖子时分秒
//
//			Log.e("!!!!!!", myFmt1.format(System.currentTimeMillis()));// 获取当前年月日

			// 获取当前年月日
			String nowDay = myFmt1.format(System.currentTimeMillis());
			// 获取该position下的帖子年月日
			String positionDay = myFmt1.format(Long.parseLong((aLists.get(position).getDateline()) + "") * 1000);
			// 获取该position下的帖子时分秒
			String positionTime = myFmt2.format(Long.parseLong((aLists.get(position).getDateline()) + "") * 1000);
			//如果是当天显示时和分， 如果超过一天显示日期
			if (nowDay.equals(positionDay)) {
				String result;
				Log.e("--------", System.currentTimeMillis()
						- Long.parseLong((aLists.get(position).getDateline()) + "") * 1000 + "");
				//3600000是一个小时的时间戳
				//如果大于一个小时只显示小时，如果小与一个小时显示分钟
				if(3600000>System.currentTimeMillis() - Long.parseLong((aLists.get(position).getDateline()) + "") * 1000){
					result = myFmt3.format(
							System.currentTimeMillis() - Long.parseLong((aLists.get(position).getDateline()) + "") * 1000);
					Log.e("--------", "小于一个小时"+result);
				}else {
					result = myFmt4.format(
							System.currentTimeMillis() - Long.parseLong((aLists.get(position).getDateline()) + "") * 1000);
					Log.e("--------", "大于一个小时"+result);
				}
				holder.update_time.setText(result);
			} else {
				holder.update_time.setText(positionDay);

			}

		
			holder.zeng_text.setText(aLists.get(position).getMessage());
			if (aLists.get(position).getHead().startsWith("http")) {
				XImageLoader.load(aLists.get(position).getHead(), holder.head_image);
			} else {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id=" + aLists.get(position).getHead(),
						holder.head_image);
			}
			holder.imgContainer.setVisibility(View.GONE);
			holder.image1.setImageBitmap(null);
			holder.image2.setImageBitmap(null);
			holder.image3.setImageBitmap(null);
			if (aLists.get(position).getImg() != null) {
				if (aLists.get(position).getImg().size() > 0) {
					if (aLists.get(position).getImg().size() > 0) {
						holder.imgContainer.setVisibility(View.VISIBLE);
						XImageLoader.load(
								GlobalConfig.API_URL + "/picture/index?id=" + aLists.get(position).getImg().get(0),
								holder.image1);
					}
					if (aLists.get(position).getImg().size() > 1)
						XImageLoader.load(
								GlobalConfig.API_URL + "/picture/index?id=" + aLists.get(position).getImg().get(1),
								holder.image2);
					if (aLists.get(position).getImg().size() > 2)
						XImageLoader.load(
								GlobalConfig.API_URL + "/picture/index?id=" + aLists.get(position).getImg().get(2),
								holder.image3);
					if (aLists.get(position).getImg().size() <= 0) {

						holder.imgContainer.setVisibility(View.GONE);
					}
				}

			} else {
				holder.imgContainer.setVisibility(View.GONE);
			}
			return convertView;
		}

		class ViewHolder {
			TextView update_time;
			TextView username_text;
			TextView zeng_text;
			TextView address_text;
			ImageView image1;
			ImageView image2;
			ImageView image3, head_image;
			LinearLayout imgContainer;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(getActivity(), DetailCommentActivity.class);
		intent.putExtra("model", aLists.get(position));
		startActivity(intent);

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity.getEventType() == TieZiPresenter.TieziFenYeSuccess) {
			aLists.clear();
			aLists.addAll(((TieZiBaoHanReply) entity.getData()).getList());
			adapter.notifyDataSetChanged();
			try {
				closeProgressDialog();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
}
